<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;  
use Illuminate\Support\Facades\Log;  
use App\Models\Camera;
use App\Models\CarRecord;
use App\Models\Project;
use App\Models\AdminUser;
use App\Models\AdminUserLog;
use App\Models\AdminUserPlan;
use App\Models\Requirement;
use App\Models\OrderColor;
use App\Models\AdminUserRequirement;
use Illuminate\Support\Facades\DB; 
use Encore\Admin\Facades\Admin;

class HomeController extends Controller
{
    public function index()  
    {  
        $now_admin_user_id = auth("adminlogin")->id();

        if(empty($now_admin_user_id)){
            $now_admin_user_id = Admin::user()->id;
            if(empty($now_admin_user_id)){
                return redirect('/admin');
            }
            
        }
        $admin_users_busy = AdminUser::whereHas('adminUserRequirements', function ($query) {  
            $query->whereIn('status', ['进行中', '未开始','停滞']);        
        })->with('adminUserRequirements', 
                'adminUserRequirements.requirement',
                'adminUserRequirements.requirement.orderColor', 
                'adminUserRequirements.requirement.module', 
                'adminUserRequirements.requirement.module.project')
                ->withCount('pendingReminds')->get();

        $admin_users_logs = AdminUserLog::where('times','<>', 0)  
            ->orderBy('date','DESC')
            ->orderBy('admin_user_id')
            ->with('adminUser',
                    'adminUserRequirement',
                    'adminUserRequirement.requirement',
                    'adminUserRequirement.requirement.module', 
                    'adminUserRequirement.requirement.module.project')
            ->get();
            $grouped_logs = [];  
        foreach ($admin_users_logs as $log) {  
            $grouped_logs[$log->date][$log->adminUser->name][] = $log;  
        }
        $admin_users_plans = AdminUserPlan::where('times','<>', 0)  
            ->orderBy('date','DESC')
            ->orderBy('admin_user_id')
            ->with('adminUser',
                    'adminUserRequirement',
                    'adminUserRequirement.requirement',
                    'adminUserRequirement.requirement.module', 
                    'adminUserRequirement.requirement.module.project')
            ->get();
            $grouped_plans = [];  
        foreach ($admin_users_plans as $plan) {  
            $grouped_plans[$plan->date][$plan->adminUser->name][] = $plan;  
        }
        $admin_users_free = AdminUser::whereDoesntHave('adminUserRequirements', function ($query) {  
        $query->whereIn('status', ['进行中', '未开始','停滞']);  
            })  
            ->with([ // 加载关系  
                'adminUserRequirements',  
                'requirements',  
                'requirements.module',  
                'requirements.module.project',
                'requirements.orderColor' 
            ])->get();
  
    $requirementsWithStatus0 = Requirement::where('status',NULL)->whereDoesntHave('adminUserRequirements', function ($query) {  
        $query->whereIn('status', ['进行中', '已完成','已归档']); 
    })->with('adminUserRequirements','orderColor','adminUserRequirements.adminUser','module', 'module.project')
    ->get();
   
        $requirementsWithStatus1 = Requirement::where('status',NULL)->whereHas('adminUserRequirements', function ($query) {  
            $query->where('status', "进行中");    
        })->with('adminUserRequirements','orderColor','adminUserRequirements.adminUser')
        ->get();  
        
       
        $requirementsWithStatus2 = Requirement::where('status',NULL)->whereDoesntHave('adminUserRequirements', function ($query) {  
            $query->whereIn('status', ['未开始', '进行中','停滞']); 
        })->with('adminUserRequirements','orderColor','adminUser','module', 'module.project')
        ->get();
        $requirements = [
            [
                'name'=>'未开始',
                'data'=>$requirementsWithStatus0
            ],
            [
                'name'=>'进行中',
                'data'=>$requirementsWithStatus1
            ],
            [
                'name'=>'停滞',
            ],
            [
                'name'=>'已完成',
                'data'=>$requirementsWithStatus2
            ],
        ];

        //停滞
        $requirement_tingzhi = AdminUserRequirement::where('status','停滞')->with('requirement.orderColor','adminUser','requirement.module', 'requirement.module.project')
        ->get();

        $data = Project::with('modules')->orderBy('order','desc')->get();

        $colors = OrderColor::all();

        return view('projects.index', ['projects' => $data,
                                        'admin_users_busy'=>$admin_users_busy,
                                        'admin_users_free'=>$admin_users_free,
                                        'requirements'=>$requirements,
                                        'colors'=>$colors,
                                        'requirement_tingzhi'=>$requirement_tingzhi,
                                        'grouped_logs'=>$grouped_logs,
                                        'now_admin_user_id'=>$now_admin_user_id,
                                        'grouped_plans'=>$grouped_plans]);
    }

    public function login()  
    {  
        return redirect('/admin');
    }
}
